package com.originskyseed.platform.pk.controller;

import java.util.Arrays;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.pk.i18n.PkResponseMsg;
import com.originskyseed.platform.pk.model.PkRule;
import com.originskyseed.platform.pk.service.IPkRuleService;
import com.originskyseed.platform.pk.vo.request.PkRuleRequestVO;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IFamilyService;

/**
 * pk规则表Controller类（OSI_PK_RULE）
 *
 * @author WuZhuoXi
 */
@Controller
@RequestMapping("/pkRule")
@BaseLocaleResource(localeFileName = "i18n_pk")
public class PkRuleController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(PkRuleController.class);

	@DubboReference
	private IPkRuleService pkRuleService;

	@DubboReference
	private IFamilyService familyService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/pkRuleList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<PkRule>> pkRuleList(HttpServletRequest request) {
		BaseResponseBody<BasePage<PkRule>> result = null;
		try {
			// 请求对象
			PkRuleRequestVO requestVO = this.getParameterBean(PkRuleRequestVO.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			// 推广平台
			if (userInfo.getSourceSys() == 2) {
				requestVO.setPlatform(2);
			}
			if (userInfo.getSourceSys() == 1) {
				requestVO.setPlatform(1);
			}
			requestVO.setIsDel(2);
			// 分页查询
			BasePage<PkRule> page = new BasePage<PkRule>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.pkRuleService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<PkRule>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), PkResponseMsg.DATA_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<PkRule>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), PkResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/pkRuleSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<PkRule> pkRuleSave(HttpServletRequest request) {
		BaseResponseBody<PkRule> result = null;
		try {
			// 请求对象
			PkRule pkRule = this.getParameterBean(PkRule.class);

			if (StringUtils.isNotBlank(pkRule.getId())) {
				this.pkRuleService.updateByPrimaryKeySelective(pkRule);
			} else {
				pkRule.setState(0);
				// pkRule.setRuleNumber(OrderNumberUtil.makeOrderNum());
				pkRule.setRuleNumber(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
				UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
				// 1,会长 4,ow
				/*
				 * if (userInfo.getSourceSys() == 2 && (userInfo.getType() == 2 || userInfo.getType() == 3 || userInfo.getType() == 4 || userInfo.getType() == 5)){
				 *
				 * }else if (userInfo.getSourceSys() == 0){ Family family = familyService.selectByUserId(userInfo.getId()); if(family!=null) { pkRule.setFamilyId(family.getId()); } }
				 */
				pkRule.setCreator(userInfo == null ? "" : userInfo.getName());
				pkRule.setCreatorId(userInfo == null ? "" : userInfo.getId());
				this.pkRuleService.insertSelective(pkRule);
			}
			result = new BaseResponseBody<PkRule>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), PkResponseMsg.SAVE_SUCCESS.getMsg()), pkRule);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<PkRule>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), PkResponseMsg.SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/pkRuleDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> pkRuleDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});
			int i=this.pkRuleService.deleteByIds(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), PkResponseMsg.DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), PkResponseMsg.DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 批量禁用启用
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/batchUpdateState.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> batchUpdateState(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String ids = this.getParameterString("ids", null);
			Integer state = this.getParameterInteger("state", null);
			if (StringUtils.isBlank(ids)) {
				return new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), PkResponseMsg.DELETE_ERROR.getMsg()), null);
			}

			this.pkRuleService.updateBatchStateByIds(Arrays.asList(ids.split(",")), state, new Date());
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), PkResponseMsg.DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), PkResponseMsg.DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 详情
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/pkRuleDetails.json", method = RequestMethod.POST)
	@RequiredParams(names = { "id" })
	public BaseResponseBody<PkRule> pkRuleDetails(HttpServletRequest request) {
		BaseResponseBody<PkRule> result = null;
		try {
			// 请求参数
			String id = this.getParameterString("id", null);

			PkRule pkRule = this.pkRuleService.selectByPrimaryKey(id);
			result = new BaseResponseBody<PkRule>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), PkResponseMsg.DATA_SUCCESS.getMsg()), pkRule);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<PkRule>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), PkResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}
}
